//
//  ContactListSource.m
//  HiQ
//
//  Created by scarlettsun on 12-8-9.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "ContactListSource.h"
#import "MacrosDefine.h"
#import "Buddy.h"
#import "BuddyGroup.h"
#import "ClusterObjects.h"
#import "ImageAndTextCell.h"

@implementation ContactListSource
@synthesize buddyGroupArray;

- (id)init
{
    buddyGroupArray = [[NSMutableArray alloc] init];
    return [super init];
}

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item
{
    if(item == nil)
        item = buddyGroupArray;
    
    if(item == buddyGroupArray)
        return [item objectAtIndex:index];
    
    if([item isKindOfClass:[BuddyGroup class]])
        return ((Buddy*)[((BuddyGroup *)item)->buddyList objectAtIndex:index]);
    if([item isKindOfClass:[ClusterGroup class]])
        return ((Cluster*)[((ClusterGroup *)item)->clusterList objectAtIndex:index]);    
    return nil;
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item
{
    if(item == nil)
        return NO;
    
    if([item isKindOfClass:[BuddyGroup class]] || [item isKindOfClass:[ClusterGroup class]])
        return YES;
    
    return NO;
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item
{
    if(item == nil)
        item = buddyGroupArray;
    
    if(item == buddyGroupArray)
        return [item count];
    
    if([item isKindOfClass:[BuddyGroup class]])
        return [((BuddyGroup *)item)->buddyList count];
    
    if([item isKindOfClass:[ClusterGroup class]])
        return [((ClusterGroup *)item)->clusterList count];   
    
    return 0;
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
    if ([[tableColumn identifier] isEqualToString: @"BuddyList"])
	{
        if(item == nil)
            return nil;
        
        if([item isKindOfClass:[BuddyGroup class]])
            return ((BuddyGroup *)item)->groupName;
        
        if([item isKindOfClass:[ClusterGroup class]])
            return ((ClusterGroup *)item)->groupName;        
    }
    return nil;
}

@end
